package remoto;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import dominio.Cliente;
import dto.ClienteTO;

public class GestionClientes {
	private static GestionClientes instance;

	public static GestionClientes getInstance() throws MalformedURLException, RemoteException, NotBoundException {
		if (instance == null)
			instance = new GestionClientes();
		return instance;
	}

	private IGestionClientes remoto;

	private GestionClientes() throws MalformedURLException, RemoteException, NotBoundException {
		remoto = (IGestionClientes) Naming.lookup("GestionClientes");
	}

	public int guardarCliente(Cliente cliente) throws RemoteException {
		return remoto.guardarCliente(cliente.getDTO());
	}

	public Cliente buscarCliente(String cuil) throws RemoteException {
		ClienteTO dto = remoto.buscarCliente(cuil);
		if (dto != null)
			return Cliente.fromDTO(dto);
		return null;
	}

	public List<Cliente> listarClientes() throws RemoteException {
		List<Cliente> lista = new ArrayList<Cliente>();
		List<ClienteTO> dtos = remoto.listarClientes();
		for (ClienteTO dto : dtos)
			lista.add(Cliente.fromDTO(dto));
		return lista;
	}
}